package org.jeecg.modules.zhima.portal.controller;

import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import org.jeecg.modules.zhima.base.common.Result;
import org.jeecg.modules.zhima.portal.service.IPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 支付管理Controller
 * Created by dingzg on 2019/1/28.
 */
@Slf4j
@Controller
@Api(tags = "PayController", description = "支付")
@RequestMapping("/pay")
public class MPayController {

    @Autowired
    private IPayService payService;

    @ApiOperation("首页内容页信息展示")
    @RequestMapping(value = "/prepay", method = RequestMethod.GET)
    @ResponseBody
    public Result<?> prepay(@RequestParam(name="orderId",required=true) Integer id) {
        log.info("==prepay==orderId=={}", id);
        if (id == null){
            return Result.error("缺少订单id");
        }
        return Result.ok(payService.prepay(id));
    }

    @ResponseBody
    @RequestMapping("/wx/notify")
    public String payNotify(HttpServletRequest request, HttpServletResponse response) {
        try {
            payService.payNotify(request);
            return WxPayNotifyResponse.success("处理成功!");
        } catch (Exception e) {
            log.error("微信回调结果异常,异常原因{}", e.getMessage());
            return WxPayNotifyResponse.fail(e.getMessage());
        }
    }

    @ApiOperation("首页内容页信息展示")
    @RequestMapping(value = "/pay", method = RequestMethod.GET)
    @ResponseBody
    public Result<?> pay() {
        payService.qrcodePay();
        return Result.ok();
    }

    @ApiOperation("退款")
    @RequestMapping(value = "/refund", method = RequestMethod.GET)
    @ResponseBody
    public Result<?> refund(@RequestParam(name="outTradeNo",required=true) String outTradeNo, @RequestParam(name="totalFee",required=true) String totalFee) {
        payService.refund(outTradeNo, totalFee);
        return Result.ok();
    }

}
